// 正则验证工具类
const regs = {
  // 邮箱格式校验
  email: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/,
  // 数字格式校验
  number: /^([0]|[1-9][0-9]*)$/,
  // 密码格式校验：密码只能由字母、数字组成
  password: /(?=.*[0-9])(?=.*[a-zA-Z])/,
}

const verify = (rule, value, reg, callback) => {
  if (value) {
    if (reg.test(value)) {
      callback()
    } else {
      callback(new Error(rule.message))
    }
  } else {
    callback()
  }
}

export default {
  email: (rule, value, callback) => {
    return verify(rule, value, regs.email, callback)
  },
  number: (rule, value, callback) => {
    return verify(rule, value, regs.number, callback)
  },
  password: (rule, value, callback) => {
    return verify(rule, value, regs.password, callback)
  },
}